<?php

namespace apiend\modules\dynamic\controllers;

use Yii;
use apiend\components\Controller;
use yii\helpers\ArrayHelper;
use apiend\modules\dynamic\models\DynamicLikeForm;
use yii\filters\AccessControl;

/**
 * 动态拓展功能
 * Default controller for the `dynamic` module
 */
class ExtendController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return ArrayHelper::merge(parent::behaviors(), [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['like'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
        ]);
    }

    /**
     * 点赞&取消点赞
     * @return string
     */
    public function actionLike() {
        $model = new DynamicLikeForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::warning('点赞请求失败，请检查传入参数有效性');
        }
        $model->user_id = $this->user->id;
        return $model->save();
    }

}
